<?php
/**
  * M_user model 文件
  * @author Cuil <lntschampion@163.com> 
  * @date 2016-02-15
  */
class M_work_task extends MY_Model{
	
	public $table = "t_job";
	function __construct() {
		parent::__construct ();
	}
    /**
    * 获取获取任务列表
    * @param string $limit
    */
    public function tasklist($where,$limit=''){
        $sql = "select * from t_job $where order by job_id ASC";
        $sql .= $limit;
        $res = $this->querylist($sql);
        return $res;
    }
    /**
    * 根据ID获取任务信息
    * @param string $limit
    */
    public function getwork($id){
        $sql = "select * from t_job where job_id=$id";
        $res = $this->db->query($sql)->row_array();
        return $res;
    }
    /**
    * 根据ID是否可删除
    * @param string $limit
    */
    public function isdelwork($id){
        $sql = "select * from t_job_process_detail where job_id=$id";
        $res = $this->db->query($sql)->row_array();
        return $res;
    }
    /**
     * 获取获取任务分类列表
     * @param string $limit
     */
    public function typelist(){
        $sql = "select id,name from t_bjnz_tasktype order by id ASC";
        $result = $this->db->query($sql)->fetchAll();
        return $result;
    }
    /**
     * 获取获取广播内容列表
     * @param string $limit
     */
    public function broadcastlist(){
        $sql = "select id,name from t_bjnz_broadcast_type order by id ASC";
        $result = $this->db->query($sql)->fetchAll();
        return $result;
    }
    /**
     * 根据类型获取人员信息
     */
    public function getpostname($type,$id_arr){
//        if($type==1)//作业人员
//        {
//            $sql = "select GROUP_CONCAT(DISTINCT user_id order by field(user_id,$id_arr)) as id,GROUP_CONCAT(DISTINCT user_name order by field(user_id,$id_arr)) as name from t_user where user_id IN($id_arr)";
//        }
//        else//作业岗位
//        {
            $sql = "select GROUP_CONCAT(DISTINCT post_name order by field(post_id,$id_arr)) as post_name from t_post where post_id IN($id_arr)";
//        }
        $result = $this->db->query($sql)->row_array();
        $result = $result['post_name'];
        return $result;
    }

    /**
     * 获取作业任务列表
     * 2017-1-9
     */
    public function getWorkTaskList(){
        $sql = "SELECT job_id,job_name FROM t_job";

        $depinfo = $this->db->query($sql)->result_array(false);

        return $depinfo;
    }

    /**
     * 获取作业岗位带负责人、作业车间
     * 2017-1-9
     */
    public function getWorkWPList(){
        $sql = "SELECT a.id as id,a.name as name, a.pname, GROUP_CONCAT(u.UserName) as cname FROM t_bjnz_work as a  LEFT JOIN t_bjnz_work_charge as b on b.wid = a.id 
			LEFT JOIN t_user u ON b.uid = u.PID GROUP BY a.id";
        $depinfo = $this->db->query($sql)->result_array(false);

        return $depinfo;
    }

    /**
     * 获取作业流程列表
     * 2017-1-9
     */
    public function getWorkProcessList(){
        $sql = "SELECT job_process_id,job_process_name FROM t_job_process";

        $depinfo = $this->db->query($sql)->result_array(false);

        return $depinfo;
    }

    /**
     * 获取作业流程详细列表
     * 2017-1-9
     */
    public function getWorkProcessDetialList(){
        $sql = "SELECT a.job_process_id as pid, a.job_process_name, b.* FROM t_job_process as a LEFT JOIN t_job_process_detail as b on b.job_process_id = a.job_process_id";

        $depinfo = $this->db->query($sql)->result_array(false);

        return $depinfo;
    }

    /**
     * 获取作业详细流程byid
     * 2017-1-9
     */
    public function getWPDetailById($id){
        $sql = "SELECT d.job_process_detail_id,d.job_id, j.job_name FROM t_job_process_detail d LEFT JOIN t_job j ON d.job_id = j.job_id WHERE d.job_process_id = '{$id}' ORDER BY d.job_process_detail_id ASC";

        $depinfo = $this->db->query($sql)->result_array(false);

        return $depinfo;
    }

    /**
     * 添加作业流程
     * @param $data 数组
     * 2017-1-9
     */
    public function insertWorkProcess($data){
        //先查询是否存在相同的标签
        $name = $data['job_process_name'];
        $sql = "select job_process_id from t_job_process where job_process_name = '{$name}'";
        $result = $this->db->query($sql)->result_array();
        if(empty($result)){
            try{
                //父表
                $sql_2 = "insert into t_job_process(job_process_name) values('{$name}')";
                $res_1 = $this->db->query($sql_2);
                $gwId = $this->db->insert_id();
                //子表
                $arr_str = $data['arr_str'];
                $sql_3 = "insert into t_job_process_detail(job_id,job_process_id) values";
                foreach ($arr_str as $k => $value) {
                    $arr_1 = explode(',', $value);
                    $sql_3 = $sql_3."('{$arr_1[1]}',{$gwId}),";
                }
                $sql_3 = rtrim($sql_3, ',');
                $this->db->query($sql_3);
                return 1;
            }catch(Exception $e){
                return 0;
            }
        }
        return '-1';
    }


    /**
     * 修改作业流程
     * @param $data 数组
     * 2017-1-10
     */
    public function updateWorkProcess($data){

        try{
            $name = $data['job_process_name'];
            $uid = $data['job_process_id'];
            //先查询是否存在相同的标签
            $sql = "select job_process_id from t_job_process where job_process_name = '{$name}'";
            $result = $this->db->query($sql)->result_array();
            foreach ($result as $va)
            {
                if(!empty($va) && $va['job_process_id']!=$uid)
                {
                    return -1;
                }
            }
            $sql = "UPDATE t_job_process SET job_process_name = '{$name}' WHERE job_process_id = '{$uid}'";
            $this->db->query($sql);

            //子表
            $flag = false;
            $ids = '';
            $arr_00 = array();
            $sql_41 = ' job_id = CASE job_process_detail_id ';

            $arr_str = $data['arr_str'];//数组
            $sql_3 = "UPDATE t_job_process_detail SET";
            $sql_insert = "INSERT into t_job_process_detail(job_id,job_process_id) values";
            foreach ($arr_str as $k => $value) {
                $arr_1 = explode(',', $value);
                if($arr_1[0] > 0){ //更新
                    $sql_41 = $sql_41."WHEN {$arr_1[0]} THEN {$arr_1[1]} ";
                    $ids .= $arr_1[0].',';
                    $arr_00[] = $arr_1[0];
                }else{ //插入
                    $flag = true;
                    $sql_insert = $sql_insert."('{$arr_1[1]}','{$uid}'),";
                }
            }
            $sql_41 .= 'END';
            //更新旧数据
            $ids = rtrim($ids, ',');
            if(!empty($ids))
            {
                $sql_3 = $sql_3.$sql_41." WHERE job_process_detail_id IN ({$ids})";
                $this->db->query($sql_3);
            }
            //先查询再插入，不然等于没添加
            $sql5 = "SELECT job_process_detail_id FROM t_job_process_detail WHERE job_process_id = '{$uid}'  ORDER BY job_process_detail_id asc";
            $arr_01 = $this->db->query($sql5)->result_array(false);
            foreach ($arr_01 as $key => $value) {
                $arr_01[$key] = $value['job_process_detail_id'];
            }
            $arr_02 = array_diff( $arr_01, $arr_00 );
            //插入新数据
            if($flag){
                $sql_insert = rtrim($sql_insert, ',');
                $this->db->query($sql_insert);
            }
            //删除前端删除的行
            if(!empty($arr_02)){
                $str = implode(',', $arr_02);
                $sql6 = "DELETE FROM t_job_process_detail WHERE job_process_detail_id IN ({$str})";
                $this->db->query($sql6);
            }
            return 1;
        }catch(Exception $e){
            return $e;
        }
    }
    /**
     * 删除作业流程
     * @param $id 作业流程id
     */
    public function deleteWorkProcess($id){
        try{
            //判断是否有列车在用该作业流程
            $sql = "select train_id from t_train where job_process_id={$id}";
            $list = $this->db->query($sql)->row_array();
            if(!empty($list['train_id']))
            {
                return 0;
            }
            $sql1 = "delete from t_job_process where job_process_id = '{$id}'";
            $this->db->query($sql1);
            $sql2 = "delete from t_job_process_detail where job_process_id = '{$id}'";
            $this->db->query($sql2);

            return 1;
        }catch(Exception $e){
            return 0;
        }
    }
}	